<template>
  <div class="cityList wrapper" ref="wrapper">
    <div>
      <div class="content">
        <p class="title">当前城市</p>
        <div class="list">
          <span class="btn current">武汉</span>
        </div>
      </div>
      <div class="hotCities">
        <p class="title">热门城市</p>
        <div class="list">
          <span
            class="btn"
            v-for="item of hotCities"
            :key="item.id"
            @click="handleClick(item)"
          >{{item.name}}</span>
        </div>
      </div>
      <div class="cities" v-for="(item,index) of cities" :key="index">
        <p class="title" :ref="index">{{index}}</p>
        <p class="item" v-for="item of item" :key="item.id" @click="handleClick(item)">{{item.name}}</p>
      </div>
    </div>
  </div>
</template>
<script>
import BScroll from "better-scroll";
export default {
  name: "CityList",
  props: ["cities", "hotCities"],
  mounted() {
    this.$nextTick(() => {
      this.scroll = new BScroll(this.$refs.wrapper, {});
    });
  },
  computed: {
    letter(){
      return this.$store.state.letter;
    }
  },
  watch: {
    letter(){
      var location = this.$refs[this.letter][0];
      this.scroll.scrollToElement(location);
    }
  },
  methods: {
    handleClick(item) {
      var city = item.name;
      this.$store.commit("changeCity", city);
      this.$router.push("/");
      this.keyword = "";
    }
  }
};
</script>
<style lang="scss" scoped>
.wrapper {
  width: 100%;
  height: 1000px;
  position: relative;
}
.cityList {
  margin-top: 102px;
  .title {
    background: #eee;
    line-height: 35px;
    padding-left: 10px;
  }
  .list {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-column-gap: 20px;
    grid-row-gap: 10px;
    padding: 20px;
    .btn {
      border: 1px solid #ddd;
      text-align: center;
      line-height: 30px;
      border-radius: 10px;
    }
    .current {
      background: #eee;
    }
  }
  .item {
    padding-left: 10px;
    line-height: 35px;
    border-bottom: 1px solid #ccc;
  }
}
</style>
